package com.takeshequ.friends;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.annotation.view.ViewInject;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.takeshequ.BasicActivity;
import com.takeshequ.Constants;
import com.takeshequ.R;
import com.takeshequ.homepage.HomePageActivity;
import com.takeshequ.main.MainFragmentActivity;
import com.utils.HttpClientUtils;
import com.utils.HttpClientUtils.HttpResponseHandler;

public class QueryFriendActivity extends BasicActivity {
	@ViewInject(id = R.id.query_friend_back_btn, click = "back")
	private Button back;

	@ViewInject(id = R.id.query_friend_search_btn, click = "search")
	private ImageView searchBtn;

	@ViewInject(id = R.id.query_friend_search_edittext)
	private EditText searchInputView;
	@ViewInject(id = R.id.query_friend_listview)
	private ListView listView;
	private List<ListItems> mListItems = new ArrayList<ListItems>();
	private PartAdapter adapter;
	private List<Friend> friends = new ArrayList<Friend>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.query_friend);
	}

	@Override
	protected void onResume() {
		super.onResume();
		adapter = new PartAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				Friend friend = ((Friend) v.getTag());
				Constants.selectMemberId = friend.getMemberId();
				Intent intent = new Intent(QueryFriendActivity.this, HomePageActivity.class);
				startActivity(intent);
			}
		});
	}

	class PartAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mListItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mListItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			return mListItems.get(position).isClickable();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (mListItems.size() > position) {
				return mListItems.get(position).getView(
						QueryFriendActivity.this, convertView,
						QueryFriendActivity.this.getLayoutInflater());
			}
			return null;
		}
	}

	private void convertToListItems() {
		mListItems.clear();
		for (Friend friend : friends) {
			ContentItem content = new ContentItem(friend);
			mListItems.add(content);
		}
	}

	public void search(View v) {
		String cont = searchInputView.getEditableText().toString().trim();
		if (StringUtils.isEmpty(cont)) {
			Toast toast = Toast.makeText(QueryFriendActivity.this, "请输入搜索条件",
					5000);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		} else {
			// AsyncHttpClient client = new AsyncHttpClient();
			Map<String, String> params = new HashMap<String, String>();
			if (StringUtils.isNumeric(cont)) {
				params.put("type", "1");
			} else {
				params.put("type", "2");
			}
			params.put("cont", cont);
			HttpClientUtils.get(QueryFriendActivity.this, "正在获取数据,请稍候",
					"queryMember", params, new HttpResponseHandler() {

						@Override
						public void handler(String response) {
							try {
								System.out.println(response);
								JSONObject jsonObject = new JSONObject(response);
								if (jsonObject.get("state").equals("1")) {

//									Member member = gson.fromJson(
//											jsonObject.getString("memberInfo"),
//											Member.class);
									Friend friend = gson.fromJson(
											jsonObject.getString("memberInfo"),
											Friend.class);
									friends.add(friend);
									convertToListItems();
									adapter.notifyDataSetChanged();
								} else {
									Toast.makeText(QueryFriendActivity.this,
											jsonObject.getString("desc"), 3000)
											.show();
								}

							} catch (JSONException e) {
								e.printStackTrace();
							}

						}
					});
		}
	}

	public void back(View v) {
		overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
		startActivity(new Intent(QueryFriendActivity.this,
				MainFragmentActivity.class));
	}

}
